<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/11/10
 * Time: 上午11:43
 */

namespace Api\Service;


class CartService extends BaseService
{
    protected $cartModel;
    public function __construct()
    {
        $this->cartModel=M('cart');
    }


    //查询购物车商品信息
    public function queryCart($uid,$filed=null){
        if(!empty($filed)){
            return $this->cartModel
                ->field($filed)
                ->join('waoo_goods on waoo_cart.goods_id=waoo_goods.goods_id')
                ->where(array('uid'=>$uid))->select();
        }else{
            return $this->cartModel
                ->field('waoo_goods.goods_pic,waoo_cart.*')
                ->join('waoo_goods on waoo_cart.goods_id=waoo_goods.goods_id')
                ->where(array('uid'=>$uid))->select();
        }
    }

    //查询购物车商品信息
    public function queryCartByWhere($where,$filed=null){
        if(!empty($filed)){
            return $this->cartModel
                ->field("waoo_goods.goods_pic,waoo_cart.*")
                ->join('waoo_goods on waoo_cart.goods_id=waoo_goods.goods_id')
                ->field($filed)->where($where)->select();
        }else{
            return $this->cartModel
                ->field("waoo_goods.goods_pic,waoo_cart.*")
                ->join('waoo_goods on waoo_cart.goods_id=waoo_goods.goods_id')
                ->where($where)->select();
        }
    }


    /**
     * 条件查询购物车信息
     * @param $where
     * @param $filed
     * @return mixed
     */
    public function findCartByWhere($where,$filed=null){
        return $this->cartModel->where($where)->find();
    }
    /**
     * 新增商品购物车
     * @param $addCart
     * @return mixed
     */
    public function addCart($addCart){
       return $this->cartModel->add($addCart);
    }

    /**修改用户购物车商品的数量
     * @param $uid
     * @param $goods_id
     * @param $num
     * @return bool
     */
    public function updCartGoodsNum($uid,$goods_id,$num){
        $where['uid']=$uid;
        $where['goods_id']=$goods_id;
        $save['goods_num']=$num;
        if($num<=0){
            return false;
        }
        return $this->cartModel->where($where)->save($save);
    }


    /**
     * 删除购物车商品
     * @param $uid
     * @param $goods_id
     * @return mixed
     */
    public function delCart($uid,$goods_id){
        $where['uid']=$uid;
        $where['goods_id']=array('in',$goods_id);
        return $this->cartModel->where($where)->delete();
    }

    /**
     * 删除购物车商品
     * @param $where
     * @return mixed
     */
    public function delCartByWhere($where){
        if(empty($where['uid'])){
            return false;
        }
        return $this->cartModel->where($where)->delete();
    }

}